package net.diaowen.dwsurvey.dao.impl;

import net.diaowen.dwsurvey.dao.AnAnswerDao;
import net.diaowen.dwsurvey.entity.Question;
import net.diaowen.dwsurvey.entity.AnAnswer;
import org.springframework.stereotype.Repository;

import net.diaowen.common.dao.BaseDaoImpl;

/**
 * 答卷 dao
 * @author keyuan(keyuan258@gmail.com)
 *
 * https://github.com/wkeyuan/DWSurvey
 * http://dwsurvey.net
 */


@Repository
public class AnAnswerDaoImpl extends BaseDaoImpl<AnAnswer, String> implements AnAnswerDao {

	@Override
	public void findGroupStats(Question question) {
		String sql="select count(case when answer='' then answer end) emptyCount, count(case when answer!='' then answer end) blankCount from t_an_answer where visibility=1 and qu_id=?";
		
		Object[] objs=(Object[]) this.getSession().createSQLQuery(sql).setString(0, question.getId()).uniqueResult();
		
		question.setRowContent(objs[0].toString());//未回答数
		question.setOptionContent(objs[1].toString());//回答的项数
		question.setAnCount(Integer.parseInt(objs[1].toString()));
	}

	@Override
	public void findGroupStatsTerm(Question question, String sex, String major) {
		String and ="";
		String join=" left join t_survey_answer an on an.id=tkts.belong_answer_id " +
				" left join t_user us on us.id=an.user_id ";
		if (!sex.equals("-1")){
			and+=" and us.sex ="+sex+" ";
		}
		if (!major.equals("-1")){
			and+=" and us.major ='" + major+"' ";
		}
		if (sex.equals("-1")&&major.equals("-1")){
			join="";
		}
		String sql="select count(case when tkts.answer='' then tkts.answer end) emptyCount, count(case when tkts.answer!='' then tkts.answer end) blankCount from t_an_answer tkts " +join+
				" where tkts.visibility=1 and tkts.qu_id=?"+and;

		Object[] objs=(Object[]) this.getSession().createSQLQuery(sql).setString(0, question.getId()).uniqueResult();

		question.setRowContent(objs[0].toString());//未回答数
		question.setOptionContent(objs[1].toString());//回答的项数
		question.setAnCount(Integer.parseInt(objs[1].toString()));
	}

}
